﻿using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Nantianmen.Core.Services;
using Nantianmen.Domain.Services.Destination;
using Nantianmen.Shared;
using System.Reflection;

namespace Nantianmen
{
    public static class NantianmenBuilderExtensions
    {
        public static NantianmenBuilder AddNantianmen(this IServiceCollection services)
        {
            if (services is null)
            {
                throw new NullReferenceException(nameof(services));
            }

            var builder = new NantianmenBuilder(services);

            builder.Services.AddEndpoints(Assembly.GetExecutingAssembly());
            builder.Services.AddScoped<NantianmenErrorDescriber>();
            builder.Services.AddTransient<IClusterService, AspNetClusterService>();
            builder.Services.AddTransient<IDestinationService, AspNetDestinationService>();
            builder.Services.AddTransient<IDestinationQueryService, AspNetDestinationService>();
            builder.Services.AddTransient<IRouteService, AspNetRouteService>();

            // http context accessor
            builder.Services.AddHttpContextAccessor();

            return builder;
        }

        public static IApplicationBuilder UseNantianmenEndpoints(this WebApplication application, RouteGroupBuilder? routeGroupBuilder = null)
        {
            application.MapEndpoints(routeGroupBuilder);

            return application;
        }
    }
}
